home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7546 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple YACC question...
  5. Date: 25 Feb 1996 12:37:52 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gqhb0INN2t7@keats.ugrad.cs.ubc.ca>
  8. References: <jeffy.824950099@eng2.iastate.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <jeffy.824950099@eng2.iastate.edu>,
  12. Jeffrey A Echtenkamp <jeffy@iastate.edu> wrote:
  13.  
  14. This is not really a topic for comp.lang.c, but I can't think of any
  15. appropriate newsgroup, so this might as well be it!
  16.  
  17. >I have a very simple yacc grammar, which gives the following warnings
  18. >under gnu bison:
  19.  
  20. Does it give warnings under yacc? Yacc is the ``standard''. :) Btw, there are
  21. newsgroups specifically for discussing GNU tools. Do you have the excellent
  22. Bison documentation?
  23.  
  24. >test.y:9:  warning:  type clash ('' 'buffer') on default action
  25. >test.y:9:  warning:  type clash ('' 'buffer') on default action
  26. >
  27. >
  28. >Here's the code:
  29. >
  30. >
  31. >%{
  32. >%}
  33. >%union 
  34. >{
  35. >  char buffer[1000];
  36. >}
  37. >%token <buffer> STRING
  38. >%%
  39. >STRINGS: STRING | STRING '.' STRINGS;
  40. >%%
  41.  
  42. You forgot to specify an action for the production for "STRING". The default
  43. one inserted by bison is apparently not compatible with they way you have
  44. attributed the STRINGS nonterminal.
  45.  
  46. As a rule, as soon as you introduce types for your non-terminals, you should
  47. provide an action to all your productions, perhaps one which assigns
  48. something meaningful to $$.
  49.  
  50. Either that, or (for now) drop the type specifier from STRING, and name it as
  51. %token STRING instead. When your grammar is ready for actions, you can type
  52. your non-terminials at the same time.
  53.  
  54. By the way, you don't have to shout to yacc or bison! While non-terminal
  55. symbols are frequently given in upper case, non-terminals (such as "strings")
  56. are named in lower case. It's just a sort of convention, but it does help you
  57. pick apart the two kinds of symbol.
  58.  
  59. >
  60. >There's alot more to the code than this, but I was able to reduce
  61. >my error down to these 10 lines and still generate the error.
  62. >The basic thing I want to say is, a "STRINGS" is either a STRING
  63. >or several strings separated by .
  64.  
  65. In that case, do add the two actions:
  66.  
  67. strings
  68.     : STRING . strings
  69.     {
  70.         /* do something */
  71.     }
  72.     | STRING
  73.     {
  74.         /* do something */
  75.     }
  76.     ;
  77.  
  78. Also note: the grammar you have written stands to blow the yacc stack during
  79. parsing.  Make it left recursive instead:
  80.  
  81. strings
  82.     : strings . STRING
  83.     | STRING
  84.     ;
  85.  
  86. This way, yacc will be able to make the reduction strings -> STRING early on.
  87. In the right-recursive model, the reductions won't happen until you see the
  88. very last STRING in sequence, thus building up the parsing stack needlessly.
  89.  
  90. >Please respond via email if possible.
  91.  
  92. CC added.
  93.  
  94. >Thanks!
  95.  
  96. No problem.
  97.  
  98. --
  99. The Kazinator, Master Yaccster.
  100. -- 
  101.  
  102.